﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.BidSystem.Model.View;

public partial class Company_BarinPriceTan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request.QueryString["cg"].ToString()))
        {
            HZ.Web.WebUtil.Alert("获取数据错误,请联系管理员。", this);
            return;
        }
        if (!IsPostBack)
        {
            DataBind();
            string company_goods = Request.QueryString["cg"].ToString();
            DataTable dt = BLL_Factory.IBLL_Company.getNoBarinPrice(company_goods);
            if (dt.Rows.Count > 0)
            {
                lblLimitPrice.Visible = false;
            }
        }
    }
    private void DataBind()
    {
        string company_goods = Request.QueryString["cg"].ToString(); 
        string CompanyID = Request.QueryString["CompanyID"].ToString();
        DataTable dt = BLL_Factory.IBLL_Company.getBarinPrice(company_goods, CompanyID);
        if (dt.Rows.Count > 0)
        {
            lblExpertPrice.Text = dt.Rows[0]["ExpertPrice"].ToString();
            lblLimitPrice.Text = dt.Rows[0]["LimitPrice"].ToString();
            if (!string.IsNullOrEmpty(dt.Rows[0]["BargainDecPrice"].ToString()))//还价价格
            {
                txtBargainDecPrice.Text = dt.Rows[0]["BargainDecPrice"].ToString();
            }
            if (!string.IsNullOrEmpty(dt.Rows[0]["BargainEncryptPrice"].ToString()))//还价理由
            {
                txtRemarks.Text = dt.Rows[0]["BargainEncryptPrice"].ToString();
            } 
        } 
    }
     
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        View_BargainPrice2 goods = new View_BargainPrice2();
        string company_goods = Request.QueryString["cg"];
        goods.BidId = company_goods;
        if (string.IsNullOrEmpty(txtBargainDecPrice.Text))
        {
            HZ.Web.WebUtil.Alert("请输入还价价格", this);
            return;
        }
        //if (string.IsNullOrEmpty(txtRemarks.Text))
        //{
        //    HZ.Web.WebUtil.Alert("请输入还价理由", this);
        //    return;
        //}
        if (txtRemarks.Text.Length > 100)
        {
            HZ.Web.WebUtil.Alert("还价理由不得超过100字。", this);
            return;
        }
        if (Convert.ToDecimal(txtBargainDecPrice.Text) == Convert.ToDecimal(new HZ.Common.ConvertHelper(lblExpertPrice.Text).ToDecimal))
        {
            HZ.Web.WebUtil.Alert("还价价格不得等于专家建议价", this);
            return;
        }
        if (string.IsNullOrEmpty(lblLimitPrice.Text)) 
        { }
        //else
        //{
        //    if (Convert.ToDecimal(txtBargainDecPrice.Text) > Convert.ToDecimal(new HZ.Common.ConvertHelper(lblLimitPrice.Text).ToDecimal))
        //    {
        //        HZ.Web.WebUtil.Alert("还价价格不得大于上限价", this);
        //        return;
        //    }
        //}

        goods.BargainDecPrice = Convert.ToDecimal(txtBargainDecPrice.Text);
        goods.BargainEncryptPrice = txtRemarks.Text;
        goods.Switch = 2;
        goods.CompanyId_Tb =Convert.ToInt32(Request.QueryString["CompanyID"]); ;
        int CheckSecces = BLL_Factory.IBLL_Company.UpdateBarinPrice(goods);
        if (CheckSecces == 1)
        {
            HZ.Common.log4netManager.LogInfo("企业已还价价格", string.Format("企业已还价价格：企业编号：{0}，报价IP：{1}，报价产品：{2}，价格：{3}。", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, company_goods, Convert.ToDecimal(txtBargainDecPrice.Text)), null);

            //Response.Write("<script>window.parent.location.reload();window.close();</script>");
            HZ.Web.WebUtil.Alert("保存成功！");

        }
        else
        {
            HZ.Web.WebUtil.Alert("确认失败，请检查数据！");
        }
    }

    //废弃方法
    protected void Button1_Click(object sender, EventArgs e)
    {
        int flag = Convert.ToInt32(ViewState["flag"].ToString());
        if (flag == 1)
        {
            Response.Write("<script>window.parent.document.getElementById('Button2').click();window.close();</script>");
        }
        else
        {
            Response.Write("<script>window.parent.location.reload();window.close();</script>");
        }
    }
}